package sunset.lucifer.ko.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import sunset.lucifer.ko.model.User;
import sunset.lucifer.ko.service.UserService;
import sunset.lucifer.ko.util.JsonUtil;

import java.util.List;

/**
 * 用户查找相关接口
 * Created by Administrator on 2016/11/21.
 */
@Controller
@Scope("prototype")
public class UserInfoController extends BaseController {

    @Autowired
    @Qualifier("userService")
    private UserService userService;

    /**
     * 获取包含用户自定义书架的用户集合
     * 请求数据：
     * {"id":1}
     *
     * @return 用户详细信息 用户自定义书架
     */
    @RequestMapping(value = "user/getUserByConditions.json", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    @ResponseBody
    public String getUserByConditions(@RequestBody String jsonCode) {
        System.out.println(jsonCode);
        User user = JsonUtil.changeJsonToObject(jsonCode, User.class);
        if (user == null) {
            return super.requestErrorResult();
        }
        System.out.println(user.toString());
        List<User> users = userService.getRecordByCondition(user);
        if (users.size() == 0) {
            System.out.println(users);
            return null;
        }
        return JsonUtil.createJsonString(users.get(0));
    }
}
